<?php
namespace YzPay\Logic;
class UserLogic extends \HomeController {
	public function _initialize() {
		parent::_initialize();

	}
//微信转帐
	function wxTransfersMoney($param) {
		$storeInfo = BoxModel("addon_wx_config")->load($this->store_id);
		$wechatObj = new \Common\Org\weiXin($storeInfo);
		$data = BoxModel("yz_member")->load($param['pay_user_id']);
		$p = array(
			'openid' => $data['openid'],
			'amount' => $param['order_amount'],
			'name' => $data['name'],
			'desc' => "订单号：" . $param['order_sn'] . "收款到帐",
		);
		$res = $wechatObj->transfersMoney($p);
		$row = array(
			'return_data' => serialize($res),
		);
		if ($res['return_code'] == 'SUCCESS' && $res['result_code']) {
			$row['payment_no'] = $res['payment_no'];
			$row['status'] = 3;
		}
		$model->where(array('order_sn' => $order_sn))->save($row);
	}

	//订单支付成功 可提现金额累加到个人帐户
	function inc_money($order_sn) {
		$model = BoxModel("yz_order");
		$order = $model->where(array('order_sn' => $order_sn))->find();
		$user_id = $order['p_user_id'];
		$user = BoxModel("yz_member")->where(array('user_id' => $user_id))->find();
		//dump($order);
		//已支付
		if ($order['status'] != 2) {
			return false;
		}
		$fee_rate = $user['fee_rate'];
		$service_fee = round(($order['order_amount'] * $fee_rate) / 100, 2); //服务费
		$avail_money = round(($order['order_amount'] - $service_fee), 2); //扣除费率之后
		$model->startTrans();
		BoxModel("system_log")->startTrans();
		$bool = $model->where(array('order_sn' => $order_sn))->save(array('status' => 3)); //订单更新成功
		$data = array(
			'user_id' => $user_id,
			'total_amount' => $order['order_amount'],
			'num' => $avail_money,
			'service_fee' => $service_fee,
			'event' => '00023',
			'order_sn' => $order_sn,
		);
		//dump($data);
		$logReturn = Logic("BoxSns", "Log")->add($data); //日志和资金增加成功
		if ($logReturn['update'] && $logReturn['logId'] && $bool !== false) {
			$model->commit();
			BoxModel("system_log")->commit();
			return true;
		} else {
			$model->rollback();
			BoxModel("system_log")->rollback();
			return false;
		}

	}
//代理增加资金
	function inc_agent_money($order_sn) {
		$model = BoxModel("yz_order");
		$order = $model->where(array('order_sn' => $order_sn))->find();
		$user_id = $order['p_user_id'];
		$user = BoxModel("yz_member")->where(array('user_id' => $user_id))->find();
		$pid_user_id = intval($user['pid']);
		if ($pid_user_id == 0) {
			return false;
		}
		$puser = BoxModel("yz_member")->where(array('user_id' => $pid_user_id))->find();
		$agent_rate = $puser['agent_rate'] > 0 ? $puser['agent_rate'] : dict('yzpay_agent_rate');
		$avail_money = round(($order['order_amount'] * $agent_rate) / 100, 2);
		$model->startTrans();
		BoxModel("system_log")->startTrans();
		$bool = $model->where(array('order_sn' => $order_sn))->save(array('agent_pay' => 1)); //已支付代理费用
		$data = array(
			'user_id' => $pid_user_id,
			'total_amount' => $order['order_amount'],
			'num' => $avail_money,
			'store_name' => $user['store_name'],
			'event' => '00024',
			'order_sn' => $order_sn,
		);
		//dump($data);
		$logReturn = Logic("BoxSns", "Log")->add($data); //日志和资金增加成功
		if ($logReturn['update'] && $logReturn['logId'] && $bool !== false) {
			$model->commit();
			BoxModel("system_log")->commit();
			return true;
		} else {
			$model->rollback();
			BoxModel("system_log")->rollback();
			return false;
		}
	}

}
